package com.example.mybatis.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatis.domain.User;
import com.example.mybatis.service.RedisService;
import com.example.mybatis.service.UserService;
import com.example.mybatis.mapper.UserMapper;
import lombok.RequiredArgsConstructor;


import org.springframework.stereotype.Service;

import java.util.List;



/**
 * @author Administrator
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-10-31 09:22:56
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    private final UserMapper userMapper;

    private final RedisService redisService;
    private static final String USER_CACHE_KEY = "user:cache:";
    private static final String USER_LIST_KEY = "user:list";
    @Override
    public User getUserById(int id) {
        String key = USER_CACHE_KEY + id;
        User user = (User) redisService.getValue(key);
        if (user == null) {
            user = userMapper.selectById(id);
            if (user != null) {
                redisService.setValue(key, user);
            }
        }
        return user;
    }

    @Override
    public List<User> getUserList() {
        List <User>list = userMapper.selectList(null);
        String key = USER_LIST_KEY;
        redisService.setValue(key, list);
        return list;
    }

    public void saveUser(User user) {
        userMapper.insert(user);
        String key = USER_CACHE_KEY + user.getId();
        redisService.setValue(key, user);
    }

    public void updateUser(User user) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, user.getId());
        userMapper.update(user, updateWrapper);
        String key = USER_CACHE_KEY + user.getId();
        redisService.setValue(key, user);
    }
    public void updateUserById(Long id, String username, String password) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        userMapper.updateById(user);
        String key = USER_CACHE_KEY + user.getId();
        redisService.setValue(key, user);
    }

    public void deleteUser(Long id) {
        userMapper.deleteById(id);
        String key = USER_CACHE_KEY + id;
        redisService.deleteKey(key);
    }


    public Page<User> getPageUserList(int currentPage, int pageSize) {

        // 尝试从 Redis 中获取用户列表
        Page<User> page = (Page<User>) redisService.getPaginatedList(USER_LIST_KEY, currentPage, pageSize, () -> {
            return userMapper.selectPage(new Page<>(currentPage, pageSize), new QueryWrapper<>());
        });

        if (page == null || page.getRecords().isEmpty()) {
            // 如果 Redis 中没有数据或数据为空，从数据库中获取
            page = userMapper.selectPage(new Page<>(currentPage, pageSize), new QueryWrapper<>());
            if (page != null && !page.getRecords().isEmpty()) {
                redisService.setPaginatedList(USER_LIST_KEY, currentPage, pageSize, page);
            }
        }
        return page;

    }
}




